package com.lry.serialize;


import com.lry.common.CommonConstants;
import com.lry.exception.FrameworkException;
import com.lry.protocol.RpcInvocation;
import com.lry.spi.LoadLevel;
import com.lry.util.ByteConvertUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


@LoadLevel(name="rpc")
public class RpcSerializer implements Serializer {



    @Override
    public <T> byte[] serialize(T t) {

        if(!(t instanceof RpcInvocation)){
            throw new FrameworkException("序列化类型必须是RpcInvocation");
        }
        RpcInvocation rpcInvocation = (RpcInvocation) t;
        ByteBuf out = Unpooled.buffer(1024);

        byte[] methodNameBytes = rpcInvocation.getTargetMethod().getBytes(CommonConstants.CHARSET);
        out.writeShort(methodNameBytes.length);
        out.writeBytes(methodNameBytes);

        byte[] serviceNameBytes = rpcInvocation.getTargetServiceName().getBytes(CommonConstants.CHARSET);
        out.writeShort(serviceNameBytes.length);
        out.writeBytes(serviceNameBytes);



        byte[] body = new byte[out.readableBytes()];
        out.readBytes(body);
        return body;
    }

    @Override
    public <T> T deserialize(byte[] data) {
        return null;
    }


}
